package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.Pet;
import com.seecen.service.PetService;
import com.seecen.mapper.PetMapper;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 桃子
* @description 针对表【pet】的数据库操作Service实现
* @createDate 2025-07-21 14:26:52
*/
@Service
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet>
    implements PetService{
    @Autowired
    private PetMapper petMapper;

    //添加动物
    @Override
    public Result addPet(Pet pet) {

        // 检查宠物是否已存在
        LambdaQueryWrapper<Pet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Pet::getPetId, pet.getPetId());
        Pet existingPet = petMapper.selectOne(wrapper);

        if (existingPet != null) {
            // 宠物已存在，执行更新操作
            existingPet.setUserId(pet.getUserId());
            existingPet.setPetName(pet.getPetName());
            existingPet.setPetType(pet.getPetType());
            existingPet.setPetGender(pet.getPetGender());
            existingPet.setPetColor(pet.getPetColor());
            existingPet.setPetBreed(pet.getPetBreed());
            existingPet.setPetAge(pet.getPetAge());

            int rows = petMapper.updateById(existingPet);
            if (rows > 0) {
                return Result.ok(null);
            } else {
                return Result.build(null, ResultCodeEnum.USERNAME_USED);
            }
        } else {
            // 宠物不存在，执行插入操作
            int rows = petMapper.insert(pet);
            if (rows > 0) {
                return Result.ok(null);
            } else {
                return Result.build(null, ResultCodeEnum.NOTLOGIN);
            }
        }
    }
    }







